<!DOCTYPE html>
<html class="x-admin-sm" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>2019-nCoV</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
        <link rel="stylesheet" href="./css/font.css">
        <link rel="stylesheet" href="./css/xadmin.css">
        <link rel="stylesheet" href="./css/my-css.css">
        <script src="./lib/layui/layui.js" charset="utf-8"></script>
        <script type="text/javascript" src="./js/xadmin.js"></script>
        <!--[if lt IE 9]>
          <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
          <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
    </head>
    <body>
        <div class="x-nav">
          <span class="layui-breadcrumb">
            <a href="./">首页</a>
            <a>
              <cite>新闻管理</cite>
            </a>
            <a>
              <cite>信息添加</cite>
            </a>
          </span>
          <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
            <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i></a>
        </div>
        <div class="layui-fluid">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-card">
                        <div class="layui-card-body">
                            <fieldset class="layui-elem-field layui-field-title">
								<legend>实时新闻</legend>
							</fieldset>
							<a class="layui-btn layui-btn layui-btn-xs layui-btn-normal" th:href="'./newsList?type='+${type}">返回</a>
                        </div>
                        <div class="layui-card-body">
                        	<form class="layui-form" action="" lay-filter="formFilter">
							<div class="layui-form-item">
								<input hidden="true" id="type" th:value="${type}"/>
							  	<div class="layui-form-item">
								    <label class="layui-form-label">新闻标题</label>
								    <div class="layui-input-inline" style="width:80%">
								    	<input type="text" name="title" lay-verify="require" autocomplete="off" placeholder="请输入标题" class="layui-input">
								    </div>
							  	</div>
							  	<div class="layui-form-item">
								    <label class="layui-form-label">新闻内容</label>
								    <div class="layui-input-block" style="width:80%">
								    	<textarea name="content" id="editId" style="display: none;"></textarea>
								    </div>
							  	</div>
							  	<div class="layui-form-item">
								    <label class="layui-form-label">媒体来源</label>
								    <div class="layui-input-inline" style="width:80%">
								    <input type="text" name="fromMedia" autocomplete="off" placeholder="请输入媒体来源" class="layui-input">
								    </div>
							  	</div>
							  	<div class="layui-form-item">
								    <label class="layui-form-label">发布时间</label>
								    <div class="layui-input-inline" style="width:80%">
								    <input type="text" name="publishTime" id="date" lay-verify="require" placeholder="yyyy-MM-dd HH:mm" autocomplete="off" class="layui-input">
								    </div>
							  	</div>
							  	<div class="layui-form-item">
								    <label class="layui-form-label">源文链接</label>
								    <div class="layui-input-inline" style="width:80%">
								    <input type="text" name="fromUrl" lay-verify="require" autocomplete="off" placeholder="请输入源文链接" class="layui-input">
								    </div>
							  	</div>
								<button type="submit" class="layui-btn layui-btn-xs" lay-submit="" lay-filter="submit-filter">创建</button>
								<button type="reset" class="layui-btn layui-btn-xs layui-bg-cyan">重置</button>
							</div>
							</form>
                        </div>
					</div>
                </div>
            </div>
        </div>
        <script th:inline="none">
			layui.use(['form', 'layedit', 'layer', 'laydate'], function(){
				var form = layui.form
				  ,layer = layui.layer
				  ,layedit = layui.layedit
				  ,laydate = layui.laydate
				  ,$ = layui.jquery;
				
				var editIndex = layedit.build('editId');
				
				laydate.render({
				    elem: '#date'
			    	,format: 'yyyy-MM-dd HH:mm'
				});
				
				form.on('submit(submit-filter)', function(data){
					data.field.content = layedit.getContent(editIndex);
					data.field.newsType = $('#type').val();
					postAjax('./../news/create', data.field, './newsList?type='+$('#type').val());
				    return false;
				});
				
			});
			
			var postAjax = function(postUrl, postData, skipHref) {
				var index =layer.load(2, { shade: [0.1,'#fff'] });
					$.ajax({
				    	url:postUrl,
				      	type:'post',
				      	dataType:'json',
				      	contentType:"application/json",
				      	async:true,
				      	cache:false,
				      	data:JSON.stringify(postData),
				      	success:function(data) {
				      		if(data.code == 0) {
				      			layer.close(index);
				      			layer.msg(data.msg, {icon: 1, time: 1000, end:function(){
					        		window.location.href = skipHref;
					        	}});
					      	} else {
					      		layer.close(index);
					        	layer.msg(data.msg, {icon: 2, time: 1000});
					        	
					      	}
				      		
						},
				  		error:function(data) {
				      		layer.close(index);
				      		layer.msg('网络异常', {icon: 7});
						}
					});
			}
			
		</script>
    </body>
</html>